﻿using HttpServer.App_Start;
using HttpServer.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/**
 * 项目需要安装几个包：
 * System.ComponentModel.Annotations
 * Microsoft.AspNet.WebApi.SelfHost
 */

namespace HttpServer
{
    public partial class FrmMain : Form
    {
        private HttpApplication _http;

        // 定义一个字典，用于存储默认的数据
        public static Dictionary<int, User> users = new Dictionary<int, User>();

        public FrmMain()
        {
            InitializeComponent();

            // 初始化HTTP启动对象，设置端口号为8087
            _http = new HttpApplication(8087);
        }

        private void FrmMian_Load(object sender, EventArgs e)
        {
            InitData();
            StartHttpServer();
        }



        /// <summary>
        /// 启动HTTP服务
        /// </summary>
        private void StartHttpServer()
        {
            _http.Run();
        }



        /// <summary>
        /// 初始化默认信息
        /// </summary>
        private void InitData()
        {
            users.Clear();
            for (int i = 1; i <= 10; i++)
            {
                User user = new User
                {
                    Id = i,
                    Name = $"User{i}",
                    Age = 20 + new Random().Next(10)
                };
                users.Add(user.Id, user);
            }
        }

    }
}
